package com.xfly.app.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.view.NoScrollListView;
import com.xfly.app.adapter.KcggAdapter;
import com.xfly.app.bean.KcggBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by MacBook on 17/3/30.
 */

public class KcDialog {
    private Onclick onclick;
    public void setonclick(Onclick onclick){
        this.onclick =onclick;
    }
    private Dialog mDialog;
    private TextView qx,qd;
    KcggAdapter mKcggAdapter;
    List<KcggBean> kcggBeanList = new ArrayList<>();
    NoScrollListView listview;
    public KcDialog(Context context, List<KcggBean> list) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_kc,null);
        for(KcggBean kcggBean:list){
            KcggBean mKcggBea= new KcggBean();
            mKcggBea.id = kcggBean.id;
            mKcggBea.name = kcggBean.name;
            mKcggBea.stock = kcggBean.stock;
            kcggBeanList.add(mKcggBea);
        }
        qx = view.findViewById(R.id.qx);
        qd = view.findViewById(R.id.qd);
        listview = view.findViewById(R.id.listview);
        mDialog = new Dialog(context, R.style.dialogDim);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
        final Window win = mDialog.getWindow();
        win.setWindowAnimations(R.style.dialogAnim);

        qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                StringBuffer gg = new StringBuffer();
                for(int i=0;i<kcggBeanList.size();i++){
                    if(mKcggAdapter.getHashMap().get(i)!=null){
                        gg.append(kcggBeanList.get(i).id+":"+mKcggAdapter.getHashMap().get(i)+"-");
                    }else {
                        gg.append(kcggBeanList.get(i).id+":"+kcggBeanList.get(i).stock+"-");
                    }
                }
                onclick.Yes(gg.toString());
            }
        });
        qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        mKcggAdapter = new KcggAdapter(context,list);
        listview.setAdapter(mKcggAdapter);

        mKcggAdapter.setClick(new KcggAdapter.Click() {
            @Override
            public void Click(int position, String num) {
                kcggBeanList.get(position).stock = num;
            }

            @Override
            public void Click2(int position) {

            }
        });
    }


    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }


    public interface Onclick{
        void Yes(String pass);
    }

    public boolean isshow(){
        if(mDialog.isShowing()){
            return true;
        }else {

            return false;
        }
    }

    public void setCanceledOnTouchOutside(boolean cacel) {
        if (null != mDialog) {
            mDialog.setCanceledOnTouchOutside(cacel);
        }
    }
}
